import urllib
import urllib2

_CLIENT_LOGIN_PATH = 'https://www.google.com/youtube/accounts/ClientLogin'

def FetchClientLoginAuthUserAndToken(email, password):
  data = urllib.urlencode({
    'Email': email,
    'Passwd': password,
    'service': 'youtube',
    'source': 'pyytup'
  })

  result = urllib2.urlopen(_CLIENT_LOGIN_PATH, data)

  username, token = '', ''
  
  for line in result.read().splitlines():
    key, val = line.split('=', 2)

    if key == 'YouTubeUser':
      username = val
    if key == 'Auth':
      token = val

  if username and token:
    return username, token
